using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class PostUploads : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.UserGeneratedContentEditor }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                postId = int.Parse(Request.QueryString["Id"]);
            }
            catch (FormatException)
            {
            }
            catch (ArgumentNullException)
            {
            }

            Acms.Bll.Post post = Acms.Bll.Post.GetById(AcmsContext.SiteId, postId, null);
            if (post != null)
            {
                postHeader.SetPost(post);
                Title = string.Format("User Uploads for '{0}'", post.DisplayName);
            }

            GridColumn colApprove = gvUploads.Columns.FindByUniqueName("colApprove");
            GridColumn colSuspend = gvUploads.Columns.FindByUniqueName("colSuspend");
            GridColumn colDelete = gvUploads.Columns.FindByUniqueName("colDelete");

            Acms.Bll.Role.Roles[] editRoles = new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.UserGeneratedContentEditor };
            if (User.HasRole(editRoles))
            {
                if(colApprove != null)
                    colApprove.Visible = true;
                if (colSuspend != null)
                    colSuspend.Visible = true;
                if (colDelete != null)
                    colDelete.Visible = true;
            }
        }
    }

    private int postId;
    protected override object SaveViewState()
    {
        ViewState["PostId"] = postId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["PostId"] is int)
            postId = (int)ViewState["PostId"];
    }


    protected void dsUploads_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UserPostUpload> e)
    {
        List<Acms.Bll.UserPostUpload> tmp = Acms.Bll.UserPostUpload.GetAllByPostId(postId);
        Acms.Bll.UserPostUploadAverage.Sort(tmp, e.SortExpression);
        e.SetData(tmp);
    }

    protected void gvUploads_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Reviewed" || e.CommandName == "Suspend" || e.CommandName == "Delete")
        {
            int id = 0;
            try
            {
                id = int.Parse(e.CommandArgument.ToString());
            }
            catch
            {
            }
            if (id > 0)
            {
                Acms.Bll.UserPostUpload tmp = Acms.Bll.UserPostUpload.GetById(id);
                if (tmp != null)
                {
                    if (e.CommandName == "Reviewed")
                    {
                        tmp.Approve();
                        tmp.Update();
                    }
                    else if (e.CommandName == "Suspend")
                    {
                        tmp.Suspend();
                        tmp.Update();
                    }
                    else if (e.CommandName == "Delete")
                    {
                        tmp.Delete();
                    }
                    gvUploads.DataBind();
                }
            }
        }
    }

    protected void gvUploads_ItemDataBound(object sender, GridItemEventArgs e)
    {
    }
}
